import { hastoken } from '@/utils/storage';
import { Redirect, Route, RouteProps } from 'react-router-dom';

export interface MyrouterProps extends RouteProps {
  component: React.FC<any>;
  path: string;
}

export type LocationStateType = {
  from: string;
};
export default function AuthRoutetr(props: MyrouterProps) {
  const { component: Comp, path, ...restProps } = props;
  return (
    <Route
      {...restProps}
      path={path}
      render={(_props) => {
        if (hastoken()) {
          return <Comp {..._props} />;
        }
        return (
          <Redirect
            to={{
              pathname: '/login',
              state: { from: path } as LocationStateType,
            }}
          />
        );
      }}
    ></Route>
  );
}
